﻿Imports System.Data.OleDb

Public Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim usr As String = HttpContext.Current.User.Identity.Name
        btn_EditProfile.NavigateUrl = String.Format("~//Profile.aspx?User={0}", usr)

        'If the logged in user is an administrator, show the link to the ManageWebsite page
        If HttpContext.Current.User.IsInRole("administrator") Then
            btn_SiteManager.Visible = True
        Else
            btn_SiteManager.Visible = False
        End If

        'Toggle side menu visibility based on Page Name - If the use is already in the profile page, he does not need the link to get there
        Dim uri As Uri = New Uri(Request.Url.ToString())
        Dim urlNoQString As String = System.IO.Path.GetFileName(uri.LocalPath) 'remove querystring from URL

        If urlNoQString = "Profile.aspx" Then
            btn_EditProfile.Visible = False
        End If

        'Toggle main menu item visibility based on USER NAME
        If HttpContext.Current.User.Identity.Name = Request.Params("User") Then
            plc_EditProfile.Visible = False
        Else
            plc_EditProfile.Visible = True
        End If

        'Check if the logged in user has been blocked by an administrator from rating or commenting
        Dim userBlocked As Boolean = False
        Dim cOleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim cSqlString As String = "SELECT * FROM PROFILE WHERE USER_UNAME=@f1"
        Dim cCmd As OleDbCommand = New OleDbCommand(cSqlString, cOleDbConn)
        cCmd.CommandType = CommandType.Text
        cCmd.Parameters.AddWithValue("@f1", HttpContext.Current.User.Identity.Name)
        cOleDbConn.Open()
        Dim cDataReader As OleDbDataReader = cCmd.ExecuteReader
        If cDataReader.HasRows Then
            cDataReader.Read()
            If Not IsDBNull(cDataReader("USER_BLCKD")) Then userBlocked = cDataReader("USER_BLCKD")
        End If
        cOleDbConn.Close()
        cOleDbConn.Dispose()


        'Toggle main menu item visibility based on ROLE
        'If the user is a registered user and not blocked, then show the Add Recipe menu item - otherwise hide it

        If (HttpContext.Current.User.IsInRole("registereduser") And (userBlocked = False)) Then
            Return
        Else
            NavigationMenu.Items.Remove(NavigationMenu.Items(6))
        End If


        'Toggle side menu visibility based on PAGE NAME - Not Used for the time being
        If System.IO.Path.GetFileName(Request.Url.ToString()) = "EditProfile.aspx" Then
            plc_MenuTags.Visible = False
        Else
            plc_MenuTags.Visible = True
        End If

        If System.IO.Path.GetFileName(Request.Url.AbsolutePath.ToString()) = "Profile.aspx" Then
            plc_MenuTags.Visible = False
        Else
            plc_MenuTags.Visible = True
        End If
        '-Till here not used

    End Sub

    Protected Sub btn_MainSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_MainSearch.Click
        Dim searchQry As String = tb_MainSearch.Text()
        Response.Redirect("/SearchResults.aspx?Search=" & searchQry)
    End Sub
End Class